#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "remotecores_error.h"

remotecores_error_t *remotecores_error_create_impl(apr_status_t err,
                                                   const char *msg,
                                                   uint32_t line,
                                                   const char *file)
{
    remotecores_error_t *e;

    e = (remotecores_error_t *) malloc(sizeof(*e));

    e->err = err;
    e->msg = strdup(msg);
    e->line = line;
    e->file = strdup(file);

    return e;
}

remotecores_error_t *remotecores_error_createf_impl(apr_status_t err,
                                                    uint32_t line,
                                                    const char *file,
                                                    const char *fmt, ...)
{
    remotecores_error_t *e;
    va_list ap;

    e = (remotecores_error_t *) malloc(sizeof(*e));

    e->err = err;

    va_start(ap, fmt);
    vasprintf((char **) &e->msg, fmt, ap);
    va_end(ap);

    e->line = line;
    e->file = strdup(file);

    return e;
}

void remotecores_error_destroy(remotecores_error_t * err)
{
    if (err) {
        free((void *) err->msg);
        free((void *) err->file);
        free(err);
    }
}
